<?php include("header.php"); ?>
<div class="wrap">
<h2>Installing 3txt</h2>
<?php
//Function's that are needed.
//Easier variable write to files
function vr($v1, $v2, $fh) {
fwrite($fh, "$v1='".$v2."';\n");
}
function vrb($v1, $v2, $fh) {
fwrite($fh, "$v1=".$v2.";\n");
}
// Variable Declaration
$domain = $_POST['domain'];
$SName = $_POST['SName'];
$SUser = $_POST['SUser'];
$SPass = md5($_POST['SPass']);
$host = $_POST['host'];
$DBName = $_POST['dbname'];
$DBUser = $_POST['dbuser'];
$DBPass = $_POST['dbpass'];
$MTBName = $_POST['MTBName'];
$theme = $_POST['theme'];
$version = "4";
$isbeta = "yes";
// Create Connection File and Make it blank, then close the file
$file = "../api/conn.php";
$fh = fopen($file, 'w');
$blank = "";
fwrite($fh, $blank);
fclose($fh);
// Re-open the connection file and start writing data to it.
$file = "../api/conn.php";
$fh = fopen($file, 'w');
// fwrite($fh, "$"."domain='".$domain."';");
fwrite($fh, "<"."?php\n");
vr('$domain', $domain, $fh);
vr('$name', $SName, $fh);
vr('$version', $version, $fh);
vr('$isbeta', $isbeta, $fh);
vr('$host', $host, $fh);
vr('$table', $MTBName, $fh);
vr('$DBName', $DBName, $fh);
vr('$DBUser', $DBUser, $fh);
vr('$DBPass', $DBPass, $fh);
vr('$theme', $theme, $fh);
vr('$SUser', $SUser, $fh);
vr('$SPass', $SPass, $fh);
vrb('$conn', "@mysql_connect('$host', '$DBUser', '$DBPass') or die ('<div>Cant Connect to Database Server</div>')", $fh);
vrb('$db', "@mysql_select_db('$DBName') or die ('<div>Cant find database</div>')", $fh);
fwrite($fh, "?".">");
fclose($fh);
chmod($file, 777);
// Create the .htaccess file
$file = "../.htaccess";
$fh = fopen($file, 'w');
fwrite($fh, "");
fclose($fh);
$file = "../.htaccess";
$fh = fopen($file, 'w');
function w($string, $fh) {
	fwrite($fh, $string."\n");
}
function replace_first($search, $replace, $data) {
    $res = strpos($data, $search);
    if($res === false) {
        return $data;
    } else {
        // There is data to be replaced
        $left_seg = substr($data, 0, strpos($data, $search));
        $right_seg = substr($data, (strpos($data, $search) + strlen($search)));
        return $left_seg . $replace . $right_seg;
    }
}  
$rb = "RewriteBase " . replace_first("http://".$_SERVER['SERVER_NAME'], "", $domain); 
if ($rb == "RewriteBase ") {
$rb = "RewriteBase /";
}
w("RewriteEngine on", $fh); w($rb, $fh); w("RewriteCond %{REQUEST_FILENAME} !-f", $fh); w("RewriteCond %{REQUEST_FILENAME} !-d", $fh); w("RewriteRule ^/?([^/\.]+)/?$ show.php?id=$1 [L]", $fh); w("RewriteRule ^/?iphone/([^/\.]+)/?$ /iphone [L]", $fh);
include("../api/conn.php");
chmod($file, 777);
echo "<div><h2>Connection File succesfully created!</h2><p>The file used to connect to the database has been created. Now I will create the tables in the database that are needed.</p></div>";
$createTB = @mysql_query("CREATE TABLE IF NOT EXISTS `$DBName`.`$MTBName` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY , `tid` TEXT NOT NULL , `text` TEXT NOT NULL , `ip` TEXT NOT NULL ) ENGINE = MYISAM ;") or die ("<div><h2>Table \"".$MTBName."\" already exists! Please drop this table and refresh this page.</h2></div>");
echo "<div><h2>The install has finished!</h2><p>Click <a href='../'>here</a> to go to your text shortner</p></div>";
include("footer.php");
?>